package com.myhost;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.myhost.utils.Convert;
import com.myhost.utils.FileSorter;

import android.os.Environment;
import android.util.Log;

public class SendFileList {

	private static SendFileList instance;
	private List<String> directoryNameList, directoryTimeList, FileNameList,
			FileTimeList, FileSizeList;
	private String sdPath;

	private SendFileList() {
		super();
		sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
	}

	public static SendFileList getSendFileList() {
		if (instance == null) {
			instance = new SendFileList();
		}
		return instance;
	}

//	 public void send(String filePath, OutputStream os) throws IOException {
//	 String path = sdPath + File.separator + filePath;
//	 getFiles(path);
//	
//	 Log.i("send", directoryNameList.size() + "----" + FileNameList.size());
//	 os.write(Convert.Int2Bytes(directoryNameList.size()));
//	 os.write(Convert.Int2Bytes(FileNameList.size()));
//	
//	 for (int i = 0; i < directoryNameList.size(); i++) {
//	 byte[] nameByte = directoryNameList.get(i).getBytes();
//	 os.write(Convert.Int2Bytes(nameByte.length));
//	 os.write(nameByte);
//	 byte[] timeByte = directoryTimeList.get(i).getBytes();
//	 os.write(Convert.Int2Bytes(timeByte.length));
//	 os.write(timeByte);
//	
//	 // Log.i("file",nameByte.length + "::" + directoryNameList.get(i) +
//	 // "----Directory");
//	 // Log.i("file",timeByte.length + "::" + directoryTimeList.get(i) +
//	 // "----Directory");
//	 }
//	 for (int i = 0; i < FileNameList.size(); i++) {
//	 byte[] nameBytef = FileNameList.get(i).getBytes();
//	 os.write(Convert.Int2Bytes(nameBytef.length));
//	 os.write(nameBytef);
//	 byte[] timeBytef = FileTimeList.get(i).getBytes();
//	 os.write(Convert.Int2Bytes(timeBytef.length));
//	 os.write(timeBytef);
//	 byte[] sizeBytef = FileSizeList.get(i).getBytes();
//	 os.write(Convert.Int2Bytes(sizeBytef.length));
//	 os.write(sizeBytef);
//	 }
//	
//	 }

	public void send(String filePath, OutputStream os, int type) throws IOException {
		String path = sdPath + File.separator + filePath;
		getFiles(path, type);

		Log.i("send", directoryNameList.size() + "----" + FileNameList.size());
		os.write(Convert.Int2Bytes(directoryNameList.size()));
		os.write(Convert.Int2Bytes(FileNameList.size()));
		int counts = AppConstant.EVERYGETCOUNTS;
		if (directoryNameList.size() < AppConstant.EVERYGETCOUNTS) {
			counts = directoryNameList.size();
		}
		for (int i = 0; i < counts; i++) {
			byte[] nameByte = directoryNameList.get(i).getBytes();
			os.write(Convert.Int2Bytes(nameByte.length));
			os.write(nameByte);
			byte[] timeByte = directoryTimeList.get(i).getBytes();
			os.write(Convert.Int2Bytes(timeByte.length));
			os.write(timeByte);
		}
		// for (int i = 0; i < FileNameList.size(); i++) {
		// byte[] nameBytef = FileNameList.get(i).getBytes();
		// os.write(Convert.Int2Bytes(nameBytef.length));
		// os.write(nameBytef);
		// byte[] timeBytef = FileTimeList.get(i).getBytes();
		// os.write(Convert.Int2Bytes(timeBytef.length));
		// os.write(timeBytef);
		// byte[] sizeBytef = FileSizeList.get(i).getBytes();
		// os.write(Convert.Int2Bytes(sizeBytef.length));
		// os.write(sizeBytef);
		// }

	}

	public void sendOtherDir(int times, int isLast, OutputStream os)
			throws IOException {
		int counts = AppConstant.EVERYGETCOUNTS;
		if (isLast == 1) {
			counts = directoryNameList.size() - times * counts;
		}
		for (int i = 0; i < counts; i++) {
			byte[] nameByte = directoryNameList.get(i + times * AppConstant.EVERYGETCOUNTS).getBytes();
			os.write(Convert.Int2Bytes(nameByte.length));
			os.write(nameByte);
			byte[] timeByte = directoryTimeList.get(i + times * AppConstant.EVERYGETCOUNTS).getBytes();
			os.write(Convert.Int2Bytes(timeByte.length));
			os.write(timeByte);
		}
	}

	public void sendOtherFile(int times, int isLast, OutputStream os) throws IOException{
		int counts = AppConstant.EVERYGETCOUNTS;
		if (isLast == 1) {
			counts = FileNameList.size() - times * counts;
		}
		for (int i = 0; i < counts; i++) {
			 byte[] nameBytef = FileNameList.get(i + times * AppConstant.EVERYGETCOUNTS).getBytes();
			 os.write(Convert.Int2Bytes(nameBytef.length));
			 os.write(nameBytef);
			 byte[] timeBytef = FileTimeList.get(i + times * AppConstant.EVERYGETCOUNTS).getBytes();
			 os.write(Convert.Int2Bytes(timeBytef.length));
			 os.write(timeBytef);
			 byte[] sizeBytef = FileSizeList.get(i + times * AppConstant.EVERYGETCOUNTS).getBytes();
			 os.write(Convert.Int2Bytes(sizeBytef.length));
			 os.write(sizeBytef);
		}
	}
	private void sendDirectory() {
		// InputStream
	}

	public void receivePathAndSend(int pathSize, InputStream is, OutputStream os, int type) {
		try {
			byte[] pathBuffer = new byte[pathSize];
			is.read(pathBuffer, 0, pathSize);
			send(new String(pathBuffer), os, type);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getFiles(String filePath, int type) {
		directoryNameList = new ArrayList<String>();
		FileNameList = new ArrayList<String>();
		directoryTimeList = new ArrayList<String>();
		FileTimeList = new ArrayList<String>();
		FileSizeList = new ArrayList<String>();
		File f = new File(filePath);
		Log.i("file", f.getName() + "---f first");
		if (f.exists() && f.canWrite()) {
			// Log.i("file", f.getName() + "---f first");
			File[] files = f.listFiles();
			Arrays.sort(files, new FileSorter(type));
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if (file.isDirectory()) {
					directoryNameList.add(file.getName());
					directoryTimeList.add(getFileLastDate(file.lastModified()));
					// Log.i("file", file.getName() + "----Directory" +
					// getFileLastDate(file.lastModified()));

				} else {
					FileNameList.add(file.getName());
					FileTimeList.add(getFileLastDate(file.lastModified()));
					FileSizeList.add(GetFileSize(file.length()));
					// Log.i("file", file.getName() + "----file");
					// Log.i("file", getFileLastDate(file.lastModified()) +
					// "-----" + GetFileSize(file.length()));
				}
			}

		}
	}

	private String getFileLastDate(long date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return sdf.format(date);
	}

	private String GetFileSize(long fileSize) {
		String result = "";
		if (fileSize >= 1024 * 1024 * 1024) {
			result = String.format("%.2f", ((double) fileSize)
					/ (1024 * 1024 * 1024))
					+ "GB";
		} else if (fileSize >= 1024 * 1024) {
			result = String.format("%.2f", ((double) fileSize) / (1024 * 1024))
					+ "MB";
		} else if (fileSize >= 1024) {
			result = String.format("%.2f", ((double) fileSize) / (1024)) + "KB";
		} else {
			result = fileSize + "Bytes";
		}
		// Log.i("file", "length--" + result);
		return result;
	}

	// private static byte[] Int2Bytes(int n) {
	// byte[] b = new byte[4];
	// b[3] = (byte) (n & 0xff);
	// b[2] = (byte) (n >> 8 & 0xff);
	// b[1] = (byte) (n >> 16 & 0xff);
	// b[0] = (byte) (n >> 24 & 0xff);
	// return b;
	// }
}
